package zheezes.socket;

import java.util.ArrayList;
import java.util.List;

import zheezes.protocol.Frame;


public class Transput {
	public static final int SUCCESS = 0;
	public static final int INTRRPT = 1;
	public static final int TIMEOUT = 2;

	private Postman postman;
	private Frame send;
	private List<Frame> recv = new ArrayList<Frame>();
	private int timeout;

	public Postman getPostman() {
		return postman;
	}

	public void setPostman(Postman postman) {
		this.postman = postman;
	}

	public Frame getSend() {
		return send;
	}

	public void setSend(Frame send) {
		this.send = send;
	}

	public Frame[] getRecv() {
		return recv.toArray(new Frame[0]);
	}

	public void addRecv(Frame f) {
		recv.add(f);
	}

	public int getTimeout() {
		return timeout;
	}

	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}

	public int await() {
		try {
			synchronized (this) {
				wait(timeout);
			}
		} catch (InterruptedException e) {
			return INTRRPT;
		}
		return recv.size() > 0 ? SUCCESS : TIMEOUT;	
	}
}
